/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-05-07     aozima       the first version
 * 2018-11-16     Ernest Chen  add finsh command and update adc function
 */

#include <rtthread.h>
#include <rtdevice.h>

#include <string.h>
#include <stdlib.h>

#include "app_nfc_update.h"
#include "app_nfc_cfg.h"
#include "drv_nfc.h"
//#include "app_sys_util.h"

//#include "cryptoauthlib.h"
//#include "host/atca_host.h"
//#include "atca_basic.h"
//#include "aes.h"

#include "app_sys_param.h"

#ifdef RT_USING_FINSH

static int nfc(int argc, char **argv)
{
    UINT8 ret,dat;
    UINT8 tab[50]={0};
    UINT8 tem[50]={0,1,2,3,4,5};

    if (argc > 1) {
        if (!strcmp(argv[1], "start")) {
            nfc_update_start();
			rt_kprintf("nfc update start\n");

        } 
		else if(!strcmp(argv[1], "info")){
            nfc_cfg_start(DRV_NFC_CMD_AES_INFO, tem, 0, 3);
            rt_thread_mdelay(500);
            ret = nfc_cfg_tab_get(tem,50);
            rt_kprintf("ret=%d,aes=%02x\n", ret,tem[0]);
            rt_kprintf("key=%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",tem[1],tem[2],tem[3],tem[4],tem[5],tem[6],tem[7],tem[8],tem[9],tem[10],tem[11],tem[12],tem[13],tem[14],tem[15],tem[16]);
	        rt_kprintf("iv= %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",tem[17],tem[18],tem[19],tem[20],tem[21],tem[22],tem[23],tem[24],tem[25],tem[26],tem[27],tem[28],tem[29],tem[30],tem[31],tem[32]);	
        }        
		else if(!strcmp(argv[1], "ver")){
            nfc_cfg_start(DRV_NFC_CMD_VER_INFO, NULL, 0, 3);
            rt_thread_mdelay(500);
            ret = nfc_cfg_tab_get(tab,50);
            rt_kprintf("ret=%d,ver=%s\n",ret,tab);
		}
		else if(!strcmp(argv[1], "aes")){
            dat=0;
            nfc_cfg_start(DRV_NFC_CMD_AES_FLAG, &dat, 1, 3);
            rt_thread_mdelay(500);
            ret = nfc_cfg_res_get();
            rt_kprintf("ret=%d\n",ret);
		}
		else if(!strcmp(argv[1], "key")){
            dat=0;
            nfc_cfg_start(DRV_NFC_CMD_AES_KEY, tem, 16, 3);
            rt_thread_mdelay(500);
            ret = nfc_cfg_res_get();
            rt_kprintf("ret=%d\n",ret);
		}
		else if(!strcmp(argv[1], "iv")){
            dat=0;
            nfc_cfg_start(DRV_NFC_CMD_AES_IV, tem, 16, 3);
            rt_thread_mdelay(500);
            ret = nfc_cfg_res_get();
            rt_kprintf("ret=%d\n",ret);
		}
		else if(!strcmp(argv[1], "test")){

                  rt_kprintf("test\n");
		}
		else if(!strcmp(argv[1], "w")){
            ret=atcab_write_bytes_zone(ATCA_ZONE_DATA, 8, 12, tem, 32);
            rt_kprintf("ret=%d\n",ret);
		}
		else if(!strcmp(argv[1], "r")){
            memset(tem,0,sizeof(tem));
            ret=atcab_read_bytes_zone(ATCA_ZONE_DATA, 8, 12, tem, 32);
            rt_kprintf("ret=%d\n",ret);
            rt_kprintf("tem1= %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",tem[0],tem[1],tem[2],tem[3],tem[4],tem[5],tem[6],tem[7],tem[8],tem[9],tem[10],tem[11],tem[12],tem[13],tem[14],tem[15]);
	        rt_kprintf("tem2= %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",tem[16],tem[17],tem[18],tem[19],tem[20],tem[21],tem[22],tem[23],tem[24],tem[25],tem[26],tem[27],tem[28],tem[29],tem[30],tem[31]);	    
		}
		else {
            rt_kprintf("Unknown command. Please enter 'nfc' for help\n");
        }    
    } 
		else {
        rt_kprintf("Usage: \n");
        rt_kprintf("nfc on  \n");
        rt_kprintf("nfc off \n");
    }
    return RT_EOK;
}
MSH_CMD_EXPORT(nfc, nfc function);

#endif /* RT_USING_FINSH */
